package com.gongJiao.controller;


import com.gongJiao.Vo.OpinionVo;
import com.gongJiao.Vo.PageCondition;
import com.gongJiao.Vo.PageResult;
import com.gongJiao.common.Result;
import com.gongJiao.entity.Site;
import com.gongJiao.entity.User;
import com.gongJiao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author Administrator
 * @since 2021-12-25
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 后台分页查询用户信息列表
     * @return
     */
    @GetMapping("getUserPageList")
    @ResponseBody
    public PageResult<User> getVehiclePageList(PageCondition pageCondition){
        PageResult<User> pageResult=new PageResult<User>();
        //查询所有数据
        List<User> list=userService.getUserList(pageCondition);
        //查询总记录数
        Integer count=userService.getCount(pageCondition);
        return pageResult.success(list,count);
    }

    /**
     * 删除用户信息
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    @ResponseBody
    public Result deleteUserByIds(@PathVariable("ids") String ids){
        userService.deleteUserByIds(ids);
        return Result.success("用户信息删除成功！");
    }

    /**
     * 判断用户名名是否可用
     * @param name
     * @return
     */
    @GetMapping("/judgeAvailable")
    @ResponseBody
    public Result judgeAvailable(String name){
        if ("".equals(name)){
            return Result.fail("用户名名不能为空");
        }
        User user=userService.getUserByName(name);
        if (user == null) {
            return Result.success("用户名可用");
        }
        return Result.fail("用户名名已存在不可用");
    }

    /**
     * 修改信息
     * @param user
     * @return
     */
    @PostMapping("updateUser")
    @ResponseBody
    public Result updateType(User user){
        User userByName= userService.getUserByName(user.getUsername());
        if (userByName == null) {
            user.setCreateTime(new Date());
            Boolean flag=userService.updateUser(user);
            if (flag) {
                return Result.success("用户信息修改成功！");
            }
            return Result.fail("用户信息修改失败！");
        }else {
            return Result.fail("用户名已存在，无法进行修改！");
        }
    }

}

